package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func PlaySelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectPlayQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectPlay(&selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func PlayFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectPlayQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindPlay(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func PlayCreate(ctx *gin.Context) {
	var addPlay validate.AddPlayWrapper

	err := ctx.ShouldBindJSON(&addPlay)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreatePlay(addPlay.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func PlayUpdate(ctx *gin.Context) {
	var updatePlay validate.UpdatePlayWrapper

	err := ctx.ShouldBindJSON(&updatePlay)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdatePlay(updatePlay)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func PlayDelete(ctx *gin.Context) {
	var deletePlayParam validate.DeletePlayParam

	err := ctx.ShouldBindJSON(&deletePlayParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&deletePlayParam.Param, "sentence")

	service.DeletePlay(&selectQueryParam)

	// 查询成功
	response.Success(ctx, nil, "success")

}

// 上传场次视频
func PlayUploadVideo(ctx *gin.Context) {
	var v validate.UploadMediaPlayParam

	err := ctx.ShouldBind(&v)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	_, fh, err := ctx.Request.FormFile("file")
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	metaData, err := DealMediaItem(ctx, fh, "")
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	err = service.UpdatePlayVideoMediaId(metaData.MediaId, v.PlayId)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	// 查询成功
	response.Success(ctx, nil, "success")
}

func PlayVideoList(ctx *gin.Context) {
	var v validate.PlayVideoList

	err := ctx.ShouldBindJSON(&v)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	total, rows, err := service.SelectPlayVideoList(v.Limit, v.Offset)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	// 查询成功
	response.Success(ctx, gin.H{"total": total, "rows": rows}, "success")
}
